package com.showgood.view.mediator {
	import com.showgood.config.PopupConfig;
	import com.showgood.core.AbstractMediator;
	import com.showgood.config.Config;
	import com.showgood.core.NotificationDispatcher;
	import com.showgood.core.NotificationEvent;
	import com.showgood.config.SangoConst;
	import com.showgood.model.ManProxy;
	import com.showgood.model.PopupStackProxy;
	import com.showgood.util.GameUtil;
	import com.showgood.util.Rect;
	import com.showgood.model.GameViewProxy;
	import com.showgood.model.LoginProxy;
	import com.showgood.vo.RemoteMessage;
	import com.showgood.vo.ReplyLoginVO;
	import com.showgood.vo.VoFactory;
	import mx.managers.PopUpManager;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.puremvc.as3.patterns.observer.Notification;
	import com.showgood.view.GameView;
	import com.showgood.view.mediator.GameViewMediator;
	import spark.components.Application;
	import com.showgood.view.GameView;
	import com.showgood.view.LoginDialog;
	
	/**
	 * 
	 * @author bianpeng
	 */
	public class ApplicatonMediator extends AbstractMediator {
		private var _app:Application;
		private var _gameView:GameView;
	
		public function ApplicatonMediator(viewObject:Object) {
			super(viewObject);
			_app = viewObject as Application;
		}
		override public function onRegister():void {
			sangoFacade.registerProxyByType(new PopupStackProxy(_app));
			sangoFacade.registerProxyByType(new ManProxy());
		}
		
		override protected function setupNotifactionHandler(dispatcher:NotificationDispatcher):void {
			dispatcher.addHandler(SangoConst.SWITCH_GAME_VIEW, switchGameView);
			dispatcher.addHandler(SangoConst.PRELOAD_RESOURCE_DONE, onPrelaodDone);
		}
		// 资源加载完毕
		private function onPrelaodDone(notify:NotificationEvent):void {
			_logger.info('sgyy client {0} started', VoFactory.VERSION);
			
			_logger.info('show login dialog');
			sangoFacade.registerProxyByType(new LoginProxy());
			
			var _p:PopupStackProxy = sangoFacade.retrieveProxyByType(PopupStackProxy) as PopupStackProxy;
			_p.pushWindow(PopupConfig.LOGIN_DIALOG).center();
			sangoFacade.registerMediatorByType(new LoginDialogMediator(_p.topwindow.window));
		}
		private function switchGameView(notify:NotificationEvent):void {
			// clear login dialog
			var _p:PopupStackProxy = sangoFacade.retrieveProxyByType(PopupStackProxy) as PopupStackProxy;
			_p.popWindow(PopupConfig.ALL_WINDOW);
			sangoFacade.removeMediatorByType(LoginDialogMediator);
			
			var app:Application = this.viewComponent as Application;
			_logger.info('enter game view');
			
			// create gameview
			var view:GameView = new GameView();
			view.percentWidth = 100;
			view.percentHeight = 100;
			app.addElement(view);			
			// 注册mediator
			var gameViewMediator: GameViewMediator = new GameViewMediator(view);
			sangoFacade.registerMediatorByType(gameViewMediator);
			// game view
			// 注册一个新的游戏视图服务
			var proxy:GameViewProxy = new GameViewProxy(view);
			sangoFacade.registerProxyByType(proxy);
			// 设置中心点
			var _vo:ReplyLoginVO = notify.data as ReplyLoginVO;
			gameViewMediator.initFirstView(
				GameUtil.calc_x(_vo.cx,	_vo.cy), 
				GameUtil.calc_y(_vo.cx,	_vo.cy));
		}
	}
}


